package com.wgq.uitest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main4Activity extends AppCompatActivity implements View.OnClickListener{

    public static int MODE = MODE_PRIVATE;
    public static final String PREFERENCE_NAME = "SaveSetting";
    Button btnLogin;
    EditText etAccount, etPassword;
    SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        btnLogin = findViewById(R.id.btn_login);
        etAccount = findViewById(R.id.account_input);
        etPassword = findViewById(R.id.password_input);

        btnLogin.setOnClickListener(this);

        sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE);
        etAccount.setText(sharedPreferences.getString("username",""));
        etPassword.setText(sharedPreferences.getString("password", ""));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("username", etAccount.getText().toString());
                editor.putString("password", etPassword.getText().toString());
                editor.commit();
                Toast.makeText(Main4Activity.this, "登录成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}